package utils
{
	import events.ScrollEvent;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import resources.Resources;
	
	import ui.components.Button;
	
    public class ScrollBar extends Sprite
    {
        private var _direction:String = "vertical";
        private var _enabled:Boolean = true;
        private var _downArrow:Button;
        private var _minScrollPosition:int = 0;
        private var _pageSize:int = 10;
        private var _maxScrollPosition:int = 150;
        private var _inDrag:Boolean;
        private var _lineScrollSize:Number = 1;
        private var _currentMouseY:int;
        private var _scrollPosition:int = 0;
        private var _height:int = 100;
        private var _width:int;
        private var _track:Button;
        private var _bar:Button;
        private var _upArrow:Button;
        private var _pageScrollSize:int = 0;

        public function ScrollBar()
        {
            initAll();
        }

        public function set lineScrollSize(param1:Number) : void
        {
            if (param1 > 0)
            {
                _lineScrollSize = param1;
            }
        }

        private function trackClickHandler(event:MouseEvent) : void
        {
            var _loc_2:Number = NaN;
            if (1)
            {
                _loc_2 = Math.max(0, Math.min(_track.height - _bar.height, mouseY - _track.y - _currentMouseY));
                setScrollPosition(_loc_2 / (_track.height - _bar.height) * (_maxScrollPosition - _minScrollPosition) + _minScrollPosition);
            }
        }

        public function get lineScrollSize() : Number
        {
            return _lineScrollSize;
        }

        override public function set width(param1:Number) : void
        {
        }

        public function get scrollPosition() : int
        {
            return _scrollPosition;
        }

        public function get minScrollPosition() : int
        {
            return _minScrollPosition;
        }

        public function get maxScrollPosition() : int
        {
            return _maxScrollPosition;
        }

        private function relayoutButtons() : void
        {
            _track.x = 0;
            _track.y = _upArrow.height;
            _track.width = _upArrow.width;
            _track.height = _height - _upArrow.height - _downArrow.height;
            _bar.width = _upArrow.width;
            _bar.height = 50;
            _bar.y = _upArrow.height;
            _downArrow.y = _height - _downArrow.height;
        }

        private function onScrollPress(event:MouseEvent) : void
        {
            if (event.currentTarget == _upArrow)
            {
                setScrollPosition(_scrollPosition - _lineScrollSize);
            }
            else if (event.currentTarget == _downArrow)
            {
                setScrollPosition(_scrollPosition + _lineScrollSize);
            }
        }

        override public function get height() : Number
        {
            return _height;
        }

        public function set scrollPosition(param1:int) : void
        {
            setScrollPosition(param1, true);
        }

        public function set enabled(param1:Boolean) : void
        {
            _enabled = param1;
            if (_enabled)
            {
                ColorUtil.highLight(this);
            }
            else
            {
                ColorUtil.deHighLight(this);
            }
            this.mouseEnabled = param1;
            this.mouseChildren = param1;
        }

        public function set minScrollPosition(param1:int) : void
        {
            setScrollProperties(_pageSize, param1, _maxScrollPosition);
        }

        public function set maxScrollPosition(param1:int) : void
        {
            setScrollProperties(_pageSize, _minScrollPosition, param1);
        }

        public function setScrollPosition(param1:int, param2:Boolean = true) : void
        {
            if (param1 > maxScrollPosition)
            {
                param1 = maxScrollPosition;
            }
            if (param1 < minScrollPosition)
            {
                param1 = minScrollPosition;
            }
            var _loc_3:* = _scrollPosition;
            _scrollPosition = Math.min(Math.max(param1, _minScrollPosition), _maxScrollPosition);
            if (param2)
            {
                this.dispatchEvent(new ScrollEvent(_direction, _scrollPosition - _loc_3, _scrollPosition));
            }
            drawBar();
        }

        private function drawBar() : void
        {
            var _loc_1:* = _maxScrollPosition - _minScrollPosition;
            if (_loc_1 <= 0)
            {
                _bar.visible = false;
            }
            else
            {
                _bar.height = Math.max(8, _track.height * (_pageSize / (_pageSize + _loc_1)));
                _bar.y = _track.y + (_track.height - _bar.height) * ((_scrollPosition - _minScrollPosition) / _loc_1);
                _bar.visible = enabled;
                this.visible = enabled;
            }
        }

        public function get enabled() : Boolean
        {
            return _enabled;
        }

        public function resetScrollPosition() : void
        {
            setScrollPosition(0);
            return;
        }

        override public function get width() : Number
        {
            return _upArrow.width;
        }

        override public function set height(param1:Number) : void
        {
            _height = param1;
            relayoutButtons();
            drawBar();
        }

        private function onStageMove(event:MouseEvent) : void
        {
            var _loc_2:Number = NaN;
            if (_inDrag)
            {
                _loc_2 = Math.max(0, Math.min(_track.height - _bar.height, mouseY - _track.y - _currentMouseY));
                setScrollPosition(_loc_2 / (_track.height - _bar.height) * (_maxScrollPosition - _minScrollPosition) + _minScrollPosition);
            }
        }

        private function initButtons() : void
        {
            _upArrow = new Button(0, "", -1, -1, Resources.SCROLL_UP);
            _downArrow = new Button(1, "", -1, -1, Resources.SCROLL_DOWN);
            _bar = new Button(3, "", -1, -1, Resources.SCROLL_BAR);
            _track = new Button(4, "", -1, -1, Resources.SCROLL_TRACK);
            
            DisplayUtil.addChild(_track, this);
            DisplayUtil.addChild(_bar, this);
            DisplayUtil.addChild(_downArrow, this);
            DisplayUtil.addChild(_upArrow, this);
            
            _upArrow.addEventListener(MouseEvent.CLICK, onScrollPress);
            _downArrow.addEventListener(MouseEvent.CLICK, onScrollPress);
            _bar.addEventListener(MouseEvent.MOUSE_DOWN, onBarDown);
            _track.addEventListener(MouseEvent.CLICK, trackClickHandler);
        }

        public function toggleTrack(param1:Boolean) : void
        {
            _track.visible = param1;
        }

        private function onStageUp(event:MouseEvent) : void
        {
            if (stage)
            {
                stage.removeEventListener(MouseEvent.MOUSE_UP, onStageUp);
                stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMove);
                _inDrag = false;
                _currentMouseY = 0;
            }
        }

        public function set pageSize(param1:int) : void
        {
            _pageSize = Math.min(param1, _maxScrollPosition - _minScrollPosition);
            drawBar();
        }

        public function setScrollProperties(param1:int, param2:int, param3:int, param4:int = 0) : void
        {
            if (param3 <= param2)
            {
                param3 = param2;
                param4 = 0;
            }
            _pageSize = param1;
            _minScrollPosition = param2;
            _maxScrollPosition = param3;
            if (param4 >= 0)
            {
                _pageScrollSize = param4;
            }
            _enabled = _maxScrollPosition > _minScrollPosition;
            setScrollPosition(_pageScrollSize);
            drawBar();
        }

        private function initAll() : void
        {
            initButtons();
            relayoutButtons();
            drawBar();
        }

        private function onBarDown(event:MouseEvent) : void
        {
            if (stage)
            {
                _currentMouseY = mouseY - _bar.y;
                stage.addEventListener(MouseEvent.MOUSE_UP, onStageUp);
                stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMove);
                _inDrag = true;
            }
        }
    }
}